package com.demo.homework;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;

public class Student implements Comparable<Student>{
    private int sid;
    private String name;
    private int age;

    public Student(int sid, String name, int age) {
        this.sid = sid;
        this.name = name;
        this.age = age;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
//根据学号排
    @Override
    public int compareTo(Student o) {
        if(this.sid!=o.sid){
            return this.sid - o.sid;
        }else{
            return this.age-o.age;
        }
    }

    public static void main(String[] args) {
        Student s1 = new Student(15, "张三", 22);
        Student s2 = new Student(15, "张三", 21);
        ArrayList<Student> objects = new ArrayList<Student>();
        objects.add(s1);
        objects.add(s2);
//         第一种排序
        Collections.sort(objects);
        System.out.println(objects);
//          第二种排序
//        if(s1.compareTo(s2)<0){
//            System.out.println(s1);
//            System.out.println(s2);
//        }else if(s1.compareTo(s2)==0){
//               System.out.println(s1);
//               System.out.println(s2);
//        }else{
//            System.out.println(s2);
//            System.out.println(s1);
//        }

    }
}
